package com.ruanyuan.springboot.service.impl;

import com.ruanyuan.springboot.dao.CarDao;
import com.ruanyuan.springboot.dao.EmployeeDao;
import com.ruanyuan.springboot.pojo.Car;
import com.ruanyuan.springboot.pojo.Employee;
import com.ruanyuan.springboot.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 车辆表业务层接口实现类
 */
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarDao CarDao;
    @Autowired
    private EmployeeDao EmployeeDao;
    /**
     * 多条件查询
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Car> selectCar(int page, int limit) {
        return this.CarDao.selectCar(page, limit);
    }

    /**
     * 根据用户id查询车辆信息
     * @param employeeid
     * @return
     */
    @Override
    public List<Car> selectCaremployeeid(String employeeid) {
        return this.CarDao.selectCaremployeeid(employeeid);
    }

    /**
     * 多条件查询个数
     * @return
     */
    @Override
    public int selectCarCount() {
        return this.CarDao.selectCarCount();
    }

    /**
     * 添加车辆信息
     * @param car
     * @return
     */
    @Override
    public int insertCar(Car car) {
        //通过用户名称查询用户信息
        Employee employee =  this.EmployeeDao.selectEmployeeUsername(car.getEmployee().getUsername());
        if(employee != null) {
            car.setEmployee(employee);
            int count = this.CarDao.insertCar(car);
            if(count > 0){
                return 1;
            }else{
                return 0;
            }
        }else{
            return -1;
        }
    }

    /**
     * 修改车辆信息
     * @param car
     * @return
     */
    @Override
    public int updateCar(Car car) {
        //通过用户名称查询用户信息
        Employee employee =  this.EmployeeDao.selectEmployeeUsername(car.getEmployee().getUsername());
        if(employee != null) {
            car.setEmployee(employee);
            int count = this.CarDao.updateCar(car);
            if(count > 0){
                return 2;
            }else{
                return 0;
            }
        }else{
            return -1;
        }
    }

    /**
     * 单个删除车辆信息
     * @param id
     * @return
     */
    @Override
    public int deleteCarById(String id) {
        return this.CarDao.deleteCarById(id);
    }

    /**
     * 批量删除车辆信息
     * @param addids
     * @return
     */
    @Override
    public int deleteCarIds(String[]  addids) {
        // 创建List集合储存id
        List<String> ids1 = new ArrayList<String>();
        // 遍历数组，储存在集合内
        for (String id : addids) {
            ids1.add(id);
        }
        return this.CarDao.deleteCarIds(ids1);
    }

    /**
     * 单个根据用户iD删除车辆信息
     * @param addid
     * @return
     */
    @Override
    public int deleteCarByEmployeeId(String addid) {
        return this.CarDao.deleteCarByEmployeeId(addid);
    }
}
